<template>
  <!-- 退料 -->
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartOutboundInternalReceiveBackPart"
      height="auto"
      :dataSource="dataSource"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
    <div style="height:20px;"></div>
    <FormPanel ref="formPanel" :list="formList" :cols="3" :isSubmitBtn="false"></FormPanel>
    <el-row class="btn-wrap">
      <el-button type="primary" size="small" @click="comfirmBtnClick">确定</el-button>
      <el-button size="small" @click="$emit('cancel')">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getStorage } from '@/api/part/partOutbound/internalReceive/index';
export default {
  name: 'wxll_back_part',
  props: {
    partInfo: Object,
    parnArr: Array
  },
  data() {
    return {
      formList: [
        {
          type: 'INPUT_NUMBER',
          label: '退料数量',
          fieldName: 'tlsl',
          precision: 2,
          initialValue: 1
        }
      ],
      columns: this.createColumns(),
      dataSource: []
    };
  },
  mounted() {
    console.log(this.partInfo, 'partInfo');
    this.getlist();
    window.wxll_back_part = this;
    this.dataSource = [this.partInfo];
  },
  methods: {
    createColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 60,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库',
          // sorter: true,
          dataIndex: 'STORAGE_CODE',
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '配件代码',
          // sorter: true,
          dataIndex: 'PART_NO'
        },
        {
          title: '配件名称',
          // sorter: true,
          dataIndex: 'PART_NAME'
        },
        {
          title: '出库数量',
          // sorter: true,
          dataIndex: 'OUT_QUANTITY'
        }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      let res = await getStorage({});
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    // 确定按钮
    comfirmBtnClick() {
      console.log('点击确定');
      const num = Number(this.formList[0].initialValue);
      if (!num) {
        this.$notify({
          title: '警告',
          message: '请输入正确的退料数量',
          type: 'warning'
        });
        return;
      }
      const backRow = { ...this.partInfo, ...{ OUT_QUANTITY: num, _uid: undefined } };
      this.$emit('confirm', backRow);
      return;
      //
      // if (num) {
      //   const num_fl = this.partInfo['OUT_QUANTITY'];
      //   if (num_fl < num) {
      //     this.$notify({
      //       title: '警告',
      //       message: '退料数量不能大于发料数量',
      //       type: 'warning'
      //     });
      //     return;
      //   }
      //   const row = Object.assign({}, this.partInfo);
      //   //数量 金额 不含税金额 成本金额
      //   row['OUT_QUANTITY'] = -num * Number(row['OUT_QUANTITY'] || 0);
      //   row['PART_SALES_AMOUNT'] = -num * Number(row['PART_SALES_AMOUNT'] || 0);
      //   row['PART_SALES_AMOUNT_NOTAX'] = -num * Number(row['PART_SALES_AMOUNT_NOTAX'] || 0);
      //   row['PART_COST_AMOUNT'] = -num * Number(row['PART_COST_AMOUNT'] || 0);
      //   this.$emit('confirm', row);
      // } else {
      //   console.log('输入不正确');
      // }
    }
  }
};
</script>

<style scoped>
.btn-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60px;
}
</style>
